<template>
	<view>
		<view class="uni-header">
			<view class="uni-group">
				<view class="uni-title"></view>
				<view class="uni-sub-title"></view>
			</view>
			<view class="uni-group">
				<button class="uni-button margin-right-sm" size="mini" @click="unverify">未审核</button>
				<input class="uni-search" type="text" v-model="query" @confirm="search" placeholder="请输入搜索内容" />
				<button class="uni-button" type="default" size="mini" @click="search">搜索</button>
				<button class="uni-button" type="default" size="mini" :disabled="!selectedIndexs.length"
					@click="delTable">批量删除</button>
				<button class="uni-button" size="mini" type="primary" :disabled="!selectedIndexs.length"
					@click="replyAll">批量回复</button>
			</view>
		</view>
		<view class="uni-container">
			<unicloud-db ref="udb" collection="cloud_goods_comments,cloud_goods,cloud_shops"
				field="content,reply,goods_id{src,title},index,order_id,shopid{name},star,starResult,tags,user,state,created"
				:where="where" page-data="replace" :orderby="orderby" :getcount="true" :page-size="options.pageSize"
				:page-current="options.pageCurrent" v-slot:default="{data,pagination,loading,error,options}"
				:options="options">
				<uni-table :loading="loading" :emptyText="error.message || '没有更多数据'" border stripe type="selection"
					@selection-change="selectionChange">
					<uni-tr>
						<uni-th align="center">用户信息</uni-th>
						<uni-th align="center">商品</uni-th>
						<uni-th align="center">评分</uni-th>
						<uni-th align="center">标签</uni-th>
						<uni-th align="center">时间</uni-th>
						<uni-th align="center">是否显示</uni-th>
						<uni-th align="center" width="300">内容</uni-th>
						<uni-th align="center" width="300">回复</uni-th>
						<uni-th width="204" align="center">操作</uni-th>
					</uni-tr>
					<uni-tr v-for="(item,index) in data" :key="index">
						<uni-td align="center">
							<view v-if="item.user">
								<image v-if="item.user.avatar" :src="item.user.avatar" mode="aspectFill"
									class="suoluetu"></image>
								<view class="">
									<text>{{item.user.nickname}}</text>
								</view>
							</view>
						</uni-td>
						<uni-td align="">
							<view class="" v-if="item.goods_id && item.goods_id.length>0">
								<view class="">
									<image :src="item.goods_id[0].src" mode="aspectFill" class="suoluetu"></image>
								</view>
								<text>{{item.goods_id[0].title}}</text>
							</view>
						</uni-td>
						<uni-td align="center">
							<view class="">
								<text> {{item.star}} </text>
							</view>
							<view class="">
								<text>{{item.starResult}}</text>
							</view>
						</uni-td>
						<uni-td align="center">
							<view v-for="(tg,index1) in item.tags" :key="index1">
								<text>{{tg}}</text>
							</view>
						</uni-td>
						<uni-td align="center">
							{{item.created | dateFormat('yyyy-MM-dd hh:mm:ss')}}
						</uni-td>
						<uni-td align="center"> {{item.state == 1?"是":"否"}} </uni-td>
						<uni-td align="center"> {{item.content}} </uni-td>
						<uni-td align="center"> {{item.reply}} </uni-td>
						<uni-td align="center">
							<view class="uni-group">
								<button @click="replyItem(item)" class="uni-button" size="mini"
									type="primary">回复</button>
								<button @click="confirmDelete(item._id)" class="uni-button" size="mini"
									type="warn">删除</button>
							</view>
						</uni-td>
					</uni-tr>
				</uni-table>
				<view class="uni-pagination-box">
					<uni-pagination show-icon :page-size="pagination.size" v-model="pagination.current"
						:total="pagination.count" @change="onPageChanged" />
				</view>
			</unicloud-db>
		</view>
		<uni-popup ref="replyPopup" type="dialog">
			<view class="bg-white padding radius order-dialog">
				<view class="title margin-bottom-sm">
					<text>回复评论</text>
				</view>
				<uni-forms ref="form" :value="formData" validateTrigger="bind">
					<uni-forms-item name="content" v-if="formData.single" label="评论内容">
						<uni-easyinput type="textarea" v-model="formData.content" :disabled="true" />
					</uni-forms-item>
					<uni-forms-item name="state" label="是否显示">
						<uni-data-checkbox v-model="formData.state" :localdata="formOptions.state_localdata" />
					</uni-forms-item>
					<uni-forms-item name="reply" label="回复内容">
						<uni-easyinput type="textarea" v-model="formData.reply" />
					</uni-forms-item>
				</uni-forms>
				<view class="uni-button-group margin-top-sm">
					<button type="primary" class="uni-button" @click="submitReply">提交</button>
					<button @click="closePop('replyPopup')" class="uni-button">关闭</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		enumConverter
	} from '../../js_sdk/validator/cloud_goods_comments.js';

	const db = uniCloud.database()
	const cmd = db.command;
	// 表查询配置
	const dbOrderBy = 'created desc' // 排序字段
	const dbSearchFields = [] // 模糊搜索字段，支持模糊搜索的字段列表
	// 分页配置
	const pageSize = 20
	const pageCurrent = 1

	import {
		mapState,
	} from 'vuex'
	export default {
		data() {
			return {
				query: '',
				where: '',
				orderby: dbOrderBy,
				selectedIndexs: [],
				options: {
					pageSize,
					pageCurrent,
					...enumConverter
				},
				formData: {
					reply: "",
					state: 0,
					content: "",
				},
				formOptions: {
					"state_localdata": [{
							"text": "未审核",
							"value": 0
						},
						{
							"text": "通过",
							"value": 1
						}, {
							"text": "不通过",
							"value": -1
						},
					],
				},
				imageStyles: {
					width: 64,
					height: 64
				}
			}
		},
		onLoad() {
			//默认所有商品
			this.where = "shopid.id==" + this.adminShop.id
		},
		computed: {
			...mapState('app', ['adminShop'])
		},
		methods: {
			getWhere() {
				const query = this.query.trim()
				if (!query) {
					return ''
				}
				const queryRe = new RegExp(query, 'i')
				return dbSearchFields.map(name => queryRe + '.test(' + name + ')').join(' || ')
			},
			unverify() {
				let conditions = ["shopid.id==" + this.adminShop.id, "state==0"];
				this.where = conditions.join(" && ");
			},
			search() {
				let newWhere = this.getWhere()
				let conditions = ["shopid.id==" + this.adminShop.id];
				if (newWhere) {
					conditions.push(`(${newWhere})`)
				}
				newWhere = conditions.join(" && ");
				const isSameWhere = newWhere === this.where
				this.where = newWhere
				if (isSameWhere) { // 相同条件时，手动强制刷新
					this.loadData()
				}
			},
			loadData(clear = true) {
				this.$refs.udb.loadData({
					clear
				})
			},
			onPageChanged(e) {
				this.$refs.udb.loadData({
					current: e.current
				})
			},
			navigateTo(url, clear) {
				// clear 表示刷新列表时是否清除页码，true 表示刷新并回到列表第 1 页，默认为 true
				uni.navigateTo({
					url,
					events: {
						refreshData: () => {
							this.loadData(clear)
						}
					}
				})
			},
			// 多选处理
			selectedItems() {
				var dataList = this.$refs.udb.dataList
				return this.selectedIndexs.map(i => dataList[i]._id)
			},
			// 批量删除
			delTable() {
				this.$refs.udb.remove(this.selectedItems())
			},
			// 多选
			selectionChange(e) {
				this.selectedIndexs = e.detail.index
			},
			confirmDelete(id) {
				this.$refs.udb.remove(id)
			},
			closePop(name) {
				this.$refs[name].close();
			},
			replyItem(item) {
				this.formData = {
					single: true,
					state: 0,
					...item
				};
				this.$refs.replyPopup.open();
			},
			replyAll() {
				this.formData = {
					multiple: true,
					state: 0,
					reply: ""
				}
				console.log(this.selectedItems())
				this.$refs.replyPopup.open();
			},
			submitReply() {
				console.log(this.formData)
				let condition = {};
				if (this.formData.single) {
					condition = {
						_id: this.formData._id
					}
				} else {
					condition = {
						_id: cmd.in(this.selectedItems())
					}
				}
				uni.showLoading({
					title: "处理中"
				})
				let data = {
					state: this.formData.state,
					operater: db.env.uid
				}
				if (this.formData.reply) {
					data.reply = this.formData.reply;
				}
				db.collection("cloud_goods_comments").where(condition).update(data).then(res => {
					uni.showToast({
						title: '回复成功'
					})
					this.closePop("replyPopup")
					this.loadData(true)
				})
			}
		}
	}
</script>
<style lang="scss">
	.order-dialog {
		min-width: 1000rpx;
	}
</style>
